package MediaOnline.WebService;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import MediaOnline.Data.ThongTinDienVien;
import MediaOnline.GUI.actActorInfo;
import MediaOnline.General.paramConst;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;

public class GetFilmByActorID {
/** Called when the activity is first created. */
    
	private static final String Soap_MethodName = "AndroidGetFilmByActorID";
	private static final String Soap_Action = paramConst.Webservice_NameSpace+Soap_MethodName;
	private static final String Soap_NameSpace = paramConst.Webservice_NameSpace;
	private static final String Soap_URL = paramConst.Webservice_Url+"FilmService.asmx?op="+Soap_MethodName;
	public Context mContext;
	public ArrayList<String> arrFilmID;
	public ArrayList<String> arrFilmName;
	public ArrayList<String> arrFilmUrl;
	public ArrayList<String> arrFilmThumbUrl;
	public SoapObject request, request2;
	public SoapSerializationEnvelope Soap_Evelope;
	public ArrayAdapter<String> adpUser;
	
	
	
	
	public boolean isOnline=false;
	//ProgressDialog progress; 
		public GetFilmByActorID(Context ctx){
			mContext = ctx;
			request = new SoapObject(Soap_NameSpace, Soap_MethodName);
			Soap_Evelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
	        //Soap_Evelope.addMapping(Soap_NameSpace, "actViewAllUser", this.getClass());
	        Soap_Evelope.dotNet=true;
	        request.addProperty("ActorID",ThongTinDienVien.ID);
	        Soap_Evelope.setOutputSoapObject(request);
	        //Khởi tạo các mảng chứa dữ liệu
	        arrFilmID = new ArrayList<String>();
	    	arrFilmName= new ArrayList<String>();
	    	arrFilmUrl= new ArrayList<String>();
	    	arrFilmThumbUrl= new ArrayList<String>();
		}
		
		public void get(){
			getActorByID c = new getActorByID();
	      	c.execute();
        }

	class getActorByID extends AsyncTask<String, Void, String> {

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			//Gan thong tin get duoc vao class ThongTinDienVien
			ThongTinDienVien.lstFilmID = arrFilmID;
			ThongTinDienVien.lstFilmName = arrFilmName;
			ThongTinDienVien.lstFilmThumbUrl = arrFilmThumbUrl;
			ThongTinDienVien.lstFilmUrl = arrFilmUrl;
			
			//Update thong tin
			actActorInfo.UpdateListFilm();
			//progress.dismiss();
			Log.d("Asynctask ::: ", "Post");			
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			Log.d("Asynctask ::: ", "Pre");
			//Khởi tạo các mảng chứa dữ liệu
	        arrFilmID = new ArrayList<String>();
	    	arrFilmName= new ArrayList<String>();
	    	arrFilmUrl= new ArrayList<String>();
	    	arrFilmThumbUrl= new ArrayList<String>();
			//Chay progress dialog
			/*progress.setMessage("Dang tai du lieu. Vui long doi!");
			progress.setIndeterminate(true);
			progress.setCancelable(false);
			progress.show();*/
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			
		}


		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			Log.d("Asynctask ::: ", "Background");
	        HttpTransportSE androidHttpTransport = new HttpTransportSE(Soap_URL);
	        
	     	//CallService
	      	try{
	      		androidHttpTransport.call(Soap_Action, Soap_Evelope);
	            //Lay du lieu file xml
	      		SoapObject requestall=(SoapObject) Soap_Evelope.getResponse();
	      		Log.d("request all::::", requestall.toString());
	            //loc cac thong tin thua
	            request = (SoapObject) requestall.getProperty(0);
	            int i = request.getPropertyCount();
	            Log.d("So dong du lieu :::::::", ""+i+" dong");
	            int x=0;
	            //Lay thong tin chi tiet
	            for (int j = 0; j < i; j++) {
	            	request2=(SoapObject) request.getProperty(j);
	            	String id = request2.getProperty("FilmID").toString();
	            	String name = request2.getProperty("FilmName").toString();
	            	String url = paramConst.URL_ROOT+request2.getProperty("Url").toString();
	            	String thumburl = paramConst.URL_ROOT+request2.getProperty("ThumbImage").toString();
	            	Log.d("Id-Name:::::", id+"--"+name);
	            	//Add vào mảng
	            	arrFilmID.add(id);
	            	arrFilmName.add(name);
	            	arrFilmUrl.add(url);
	            	arrFilmThumbUrl.add(thumburl);
	            	
				}	        
			}
			catch(Exception e){
				Log.d("Asynctask exception ::: ", e.toString());

			}
			return null;
		}
		
	}
}
